﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenefitHelper.View
{
    /// <summary>
    /// 交易员持仓时间和收益率关系图
    /// </summary>
    public class OperatorYieldInHoldTime
    {
        public int Id { get; set; }

        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        public int TradeHistoryId { get; set; }

        public virtual Sys.TradeHistory TradeHistory { get; set; }
        /// <summary>
        /// 持仓时间
        /// </summary>
        public int HoldTime { get; set; }
        /// <summary>
        /// 收益率
        /// </summary>
        public int Yield { get; set; }







        public List<OperatorYieldInHoldTime> GetOperatorYieldInHoldTime(int tradeHistoryId, int operatorId, DB.DBManager db)
        {
            List<OperatorYieldInHoldTime> times = new List<OperatorYieldInHoldTime>();
            List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(new Sys.TradeHistory().GetDateTimeFromTradeHistoryId(tradeHistoryId, db), operatorId, db);
            foreach (Sys.Account account in accounts)
            {
                List<Data.Day.YieldInHoldTime> lists = new Data.Day.YieldInHoldTime().GetYieldInHoldTime(tradeHistoryId, account.Id, db);
                foreach (Data.Day.YieldInHoldTime list in lists)
                {
                    var query = times.Where(a => a.HoldTime == list.HoldTime);
                    if (query.Count() > 0)
                    {
                        var obj = query.First();
                        obj.Yield += list.Yield;
                    }
                    else
                    {
                        OperatorYieldInHoldTime time = new OperatorYieldInHoldTime();
                        time.HoldTime = list.HoldTime;
                        time.OperatorId = operatorId;
                        time.Yield = list.Yield;
                        times.Add(time);
                    }
                }
            }
            times.Sort((x, y) => x.HoldTime.CompareTo(y.HoldTime));
            return times;
        }
    }
}
